//********************************************************************************************************************//
//ORGANIZATION:
//AUTHOR: bpeng(bogers.peng@gmail.com)
//SUMMARY:
//********************************************************************************************************************//
#ifndef NATIVE_SYSTEM_BASE_MAPCURSOR_H
#define NATIVE_SYSTEM_BASE_MAPCURSOR_H

#ifndef NATIVE_SYSTEM_BASE_OBJECT_H
#include "System/Base/Object.h"
#endif
#ifndef NATIVE_SYSTEM_BASE_EQUALTO_H
#include "System/Base/EqualTo.h"
#endif
#ifndef NATIVE_SYSTEM_BASE_CURSORIAL_H
#include "System/Base/Cursorial.h"
#endif

namespace Native { namespace System
{
    //****************************************************************************************************************//
    //CMapCursor
    //
    //****************************************************************************************************************//
    template<typename _TKey, typename _TValue>
    class NATIVE_LIBRARY_EXPORT CMapCursor :
        public CObject,
        public ICursorial,
        public IEqualTo<CMapCursor<_TKey, _TValue>>
    {
        NATIVE_DECLARE_OBJECT;

        NATIVE_REDEFINE(public, _TKey, TKey, T_KEY);
        NATIVE_REDEFINE(public, _TValue, TValue, T_VALUE);
        NATIVE_REDEFINE(public, CMapCursor<TKey COMMA TValue>, Class, CLASS);
        typedef typename map<TKey, TValue>::const_iterator ConstIteratorStl;

    protected:
        bool _exist;
        ConstIteratorStl _iterator;

    public:
        CMapCursor() :
            _exist(false)
        {};

        CMapCursor(CLASS& that) :
            CObject(that),
            _exist(that._exist),
            _iterator(that._iterator)
        {};

        CMapCursor(CLASS&& that) :
            CObject(that),
            _exist(that._exist),
            _iterator(that._iterator)
        {};

        CMapCursor(ConstIteratorStl& iterator) :
            _exist(true),
            _iterator(iterator)
        {};

    public:
        virtual bool operator==(CLASS& rvalue) const
        {
            return _exist == rvalue._exist && _iterator == rvalue._iterator;
        };

        virtual bool operator!=(CLASS& rvalue) const
        {
            return !operator==(rvalue);
        };

        virtual bool Exist() const
        {
            return _exist;
        };

        virtual bool Forward()
        {
            if (Exist()) {
                _iterator++;

                return true;
            } else {
                return false;
            }
        };

        virtual bool Back()
        {
            if (Exist()) {
                _iterator--;

                return true;
            } else {
                return false;
            }
        };

        TKey Key() const
        {
            return _iterator->first;
        };

        virtual CLASS& operator=(CLASS& rvalue)
        {
            _exist = rvalue._exist;
            _iterator = rvalue._iterator;

            return *this;
        };
    };
}}

#endif